"""
Case Type   : 功能测试
Case Name   : CREATE FOREIGN TABLE ...SERVER ...
Create At   : 2022/7/4
Owner       : zou_jialiang050
Description :
    1.设置guc参数
    2.创建外部数据包装器、server、外表
    3.清理环境
Expect      :
    1.成功
    2.成功
    3.清理环境成功
History     :
"""


import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Table(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.table = 't_table_test_0285'

    def test_table(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = '-----step1:设置guc参数; expect:成功-----'
        self.logger.info(step)
        res = self.primary_sh.execute_gsguc('set', '',
                                            'support_extended_features=on',
                                            get_detail=True)
        self.logger.info(res)
        self.assertIn(self.Con.GSGUC_SUCCESS_MSG, res, "执行失败" + step)

        res = self.primary_sh.restart_db_cluster(get_detail=True)
        self.logger.info(res)
        self.assertIn(self.Con.START_SUCCESS_MSG, res, '执行失败')

        step = '-----step2:创建外部数据包装器、server、外表;expect:成功-----'
        self.logger.info(step)
        sql = f'drop schema if exists sch_table_0287;' \
              f'create schema sch_table_0287;' \
              f'drop foreign data wrapper if exists fdw_table_0287;' \
              f'create foreign data wrapper fdw_table_0287;' \
              f'drop server if exists ser_table_0287;' \
              f'create server ser_table_0287 ' \
              f'foreign data wrapper fdw_table_0287;' \
              f'drop foreign table if exists sch_table_0287.t_table_0287;' \
              f'create foreign table sch_table_0287.t_table_0287 (' \
              f'a int, b int, c text) server ser_table_0287;'
        res = self.primary_sh.execut_db_sql(sql)
        self.logger.info(res)
        self.assertIn(self.Con.CREATE_SCHEMA_SUCCESS_MSG, res, "执行失败" + step)
        self.assertIn('CREATE FOREIGN DATA WRAPPER', res, "执行失败" + step)
        self.assertIn(self.Con.create_server_success, res, "执行失败" + step)
        self.assertIn(self.Con.CREATE_FOREIGN_SUCCESS_MSG, res,
                      "执行失败" + step)

    def tearDown(self):
        step = '-----step5:清理环境；expect:成功-----'
        self.logger.info(step)
        sql = f'drop foreign table if exists sch_table_0287.t_table_0287;' \
              f'drop server if exists ser_table_0287;' \
              f'drop schema if exists sch_table_0287;' \
              f'drop foreign data wrapper if exists fdw_table_0287;'
        res3 = self.primary_sh.execut_db_sql(sql)
        self.logger.info(res3)

        res = self.primary_sh.execute_gsguc('set', '',
                                            'support_extended_features=off',
                                            get_detail=True)
        self.logger.info(res)

        res2 = self.primary_sh.restart_db_cluster(get_detail=True)
        self.logger.info(res2)

        self.assertIn(self.Con.DROP_FOREIGN_SUCCESS_MSG, res3, "执行失败" + step)
        self.assertIn(self.Con.drop_server_success, res3, "执行失败" + step)
        self.assertIn(self.Con.DROP_SCHEMA_SUCCESS_MSG, res3, "执行失败" + step)
        self.assertIn('DROP FOREIGN DATA WRAPPER', res3, "执行失败" + step)
        self.assertIn(self.Con.GSGUC_SUCCESS_MSG, res, "执行失败" + step)
        self.assertIn(self.Con.START_SUCCESS_MSG, res2, '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
